package com.qianzhou.eduWeb.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qianzhou.dto.UserDTO;
import com.qianzhou.eduService.service.UserService;
import com.qianzhou.entity.User;
import com.qianzhou.vo.ResponseVO;
import com.qianzhou.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


/**
 * 描述：UserController层 -- 用户接口
 */
@Slf4j
@RestController
@RequestMapping("user")
@Api(tags = {"用户接口"})
public class UserController {
    @Reference
    private UserService userService; // 远程消费

    @ApiOperation(value = "获取用户信息接口")
    @RequestMapping(value = "getUser/{id}", method = {RequestMethod.GET})
    public User getUser(@PathVariable("id") Integer id) {
        return userService.getUser(id);
    }

    @ApiOperation(value = "用户登录接口")
    @RequestMapping(value = "login", method = {RequestMethod.POST})
    public ResponseVO<Object> login(@RequestBody UserDTO dto) {
        UserVO user;
        // 检查手机号是否注册
        Integer phone = userService.checkPhone(dto.getPhone());
        if (phone == 0) {
            // 未注册，自动注册并登录
            userService.register(dto);
            /*dto.setMessage("手机号尚未注册，系统已帮您自动注册，请牢记密码！");*/
            user = userService.login(dto.getPhone(), dto.getPassword());
            return new ResponseVO<>(2000, "手机号尚未注册，系统已帮您自动注册，请牢记密码！", user);
        } else {
            user = userService.login(dto.getPhone(), dto.getPassword());
            if (user == null) {
                return new ResponseVO<>(500, "帐号密码不匹配，登录失败！", null);
            } else {
                return new ResponseVO<>(2000, "登录成功！", user);
            }
        }
    }
}
